package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书籍");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        int index = -1;

        //遍历数组找到有没有要的书 如果找到 index = i 跳出循环
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("没有要删的书");
            return;
        }

        //删除书籍
        for (int i = index; i < currentSize - 1; i++) {
            Book book = bookList.getBooks(i + 1);
            bookList.setBooks(i, book);
        }
        System.out.println("删除成功");
        //删除后，书的数要 - 1
        bookList.setUsedSize(currentSize - 1);
    }
}
